import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/controller/books.dart';
import 'package:ichinesereader/utils/helper.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/buttons/debouncedMaterialButton.dart';

class MyWriting extends StatefulWidget {
  MyWriting({Key? key, this.bookId, this.assignmentId}) : super(key: key);
  final String? bookId;
  final String? assignmentId;
  _MyWriting createState() => new _MyWriting();
}

class _MyWriting extends State<MyWriting> {
  final writingController = TextEditingController();
  BooksController _booksController = Get.find();

  void _saveWriting() {
    _booksController.saveWriting(
        widget.bookId, widget.assignmentId, writingController.text);
    _booksController.setCurrentWriting(writingController.text);
  }

  void _shareWriting() {
    _booksController.shareWriting(
        widget.bookId, widget.assignmentId, writingController.text);
    _booksController.setCurrentWriting(writingController.text);
  }

  void _setCurrentWriting() {
    _booksController.setCurrentWriting(writingController.text);
  }

  @override
  void initState() {
    super.initState();
    _booksController
        .getWriting(widget.bookId, widget.assignmentId)
        .then((writing) {
      if (_booksController.myCurentWriting.value != '') {
        writingController.text = _booksController.myCurentWriting.value;
      } else {
        writingController.text = writing;
      }
    });
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('Build book writing page');
    return AlertDialog(
      title: Text('Writing'.tr),
      titleTextStyle: TextStyle(
          fontWeight: FontWeight.bold,
          letterSpacing: 1,
          color: Get.isDarkMode ? Color(0xffcccccc) : Colors.black,
          fontSize: 20),
      backgroundColor: Get.isDarkMode
          ? (Color(0xff333333))
          : Color.fromARGB(255, 234, 241, 238),
      shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.all(Radius.circular(20))),
      content: Container(
        height: 360,
        width: MediaQuery.of(context).size.width * 0.80,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Container(
                  height: 300,
                  width: MediaQuery.of(context).size.width * 0.75,
                  //alignment: Alignment.center,
                  child: TextField(
                    controller: writingController,
                    maxLines: 29,
                    maxLength: 1000,
                    decoration: InputDecoration(
                      border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(8.0)),
                    ),
                  ),
                ),
              ],
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                MaterialButton(
                    color:
                        Get.isDarkMode ? (Color(0xff444444)) : (Colors.white),
                    textColor: Get.isDarkMode
                        ? (Color(0xffcccccc))
                        : Theme.of(context).primaryColorDark,
                    onPressed: () {
                      _setCurrentWriting();
                      Navigator.of(context).pop();
                    },
                    child: Text('Close'.tr)),
                SizedBox(
                  width: 20,
                ),
                DebouncedMaterialButton(
                    debounceTimeMs: 1800,
                    color: Get.isDarkMode
                        ? (Colors.white)
                        : Theme.of(context).primaryColorDark,
                    textColor: Get.isDarkMode
                        ? (Colors.black)
                        : Theme.of(context).primaryColorLight,
                    onPressed: () {
                      _saveWriting();
                      Future.delayed(Duration(milliseconds: 5000), (() {
                        String? message =
                            _booksController.statusMessageResult.value.message!;
                        if (_booksController.statusMessageResult.value.status ==
                            1000) {
                          showSnackMsg(message);
                          // Get.snackbar('Message'.tr, message,
                          //     backgroundColor: Get.theme.primaryColor,
                          //     colorText: Get.theme.primaryColorLight,
                          //     snackPosition: SnackPosition.BOTTOM);
                          Navigator.of(Get.context as BuildContext).pop();
                        } else {
                          showSnackMsg(message);
                        }
                      }));
                    },
                    child: Text('Save'.tr)),
                SizedBox(
                  width: 20,
                ),
                DebouncedMaterialButton(
                    debounceTimeMs: 1200,
                    color: Get.isDarkMode
                        ? (Colors.white)
                        : Theme.of(context).primaryColorDark,
                    textColor: Get.isDarkMode
                        ? (Colors.black)
                        : Theme.of(context).primaryColorLight,
                    onPressed: () {
                      _shareWriting();
                      Future.delayed(Duration(milliseconds: 2500), (() {
                        String? message =
                            _booksController.statusMessageResult.value.message!;
                        if (_booksController.statusMessageResult.value.status ==
                            1000) {
                          showSnackMsg(message);
                          // Get.snackbar('Message'.tr, message,
                          //     backgroundColor: Get.theme.primaryColor,
                          //     colorText: Get.theme.primaryColorLight,
                          //     snackPosition: SnackPosition.BOTTOM);
                          Navigator.of(Get.context as BuildContext).pop();
                        } else {
                          showSnackMsg(message);
                        }
                      }));
                    },
                    child: Text('Share'.tr))
              ],
            ),
          ],
        ),
      ),
    );
  }
}
